package com.example.admin.goodnews.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.entity.MessageEntity;
import com.example.admin.goodnews.interfaces.RecyclerViewItemLongClickListener;

import java.util.List;

/**
 * 我的聊天界面的适配器
 * Created by admin on 2016/10/21.
 */

public class ChatAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context context;
    private List<MessageEntity> chatlist;
    private int type;

    public ChatAdapter(Context context, List<MessageEntity> chatlist) {
        this.context = context;
        this.chatlist = chatlist;
    }

    public static enum ITEM_TYPE {
        ITEM_TYPE_SEND,
        ITEM_TYPE_RECIVE
    }

    @Override
    public int getItemViewType(int position) {
        type = chatlist.get(position).getType();
        return type;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case 1:
                View view_mine = LayoutInflater.from(context).inflate(R.layout.item_chat_mine, null);
                ViewHolder_mine holder_mine = new ViewHolder_mine(view_mine);
                return holder_mine;
            case 0:
                View view_robot = LayoutInflater.from(context).inflate(R.layout.item_chat_robot, null);
                ViewHolder_robot holder_robot = new ViewHolder_robot(view_robot);
                return holder_robot;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder_mine) {
            ((ViewHolder_mine) holder).tv_message_mine.setText(chatlist.get(position).getMessage());
        }
        if (holder instanceof ViewHolder_robot) {
            ((ViewHolder_robot) holder).tv_message_robot.setText(chatlist.get(position).getMessage());
        }
    }

    @Override
    public int getItemCount() {
        return chatlist.size();
    }


    // 我发送消息的ViewHolder
    public class ViewHolder_mine extends RecyclerView.ViewHolder {
        private TextView tv_message_mine;// 我发送的消息

        public ViewHolder_mine(View itemView) {
            super(itemView);
            tv_message_mine = (TextView) itemView.findViewById(R.id.tv_message_mine);
        }
    }

    // 机器人回复消息的ViewHolder
    public class ViewHolder_robot extends RecyclerView.ViewHolder {
        private TextView tv_message_robot;// 我发送的消息

        public ViewHolder_robot(View itemView) {
            super(itemView);
            tv_message_robot = (TextView) itemView.findViewById(R.id.tv_message_robot);
        }
    }
}
